package com.ktg.mes.qc.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.qc.mapper.QcItemTemplateMapper;
import com.ktg.mes.qc.domain.QcItemTemplate;
import com.ktg.mes.qc.service.IQcItemTemplateService;



/**
 * 检测模板-产品检测汇总Service业务层处理
 *
 * @author byteco
 * @date 2022-12-06
 */
@Service
public class QcItemTemplateServiceImpl extends ServiceImpl<QcItemTemplateMapper, QcItemTemplate> implements IQcItemTemplateService
{




    /**
     * 查询检测模板-产品检测汇总
     *
     * @param entity 检测模板-产品检测汇总
     * @param page 分页对象
     * @return 检测模板-产品检测汇总
     */
    @Override
    public IPage<QcItemTemplate> queryPageList(QcItemTemplate entity, Page<QcItemTemplate> page) {
        QueryWrapper<QcItemTemplate> lqw = new QueryWrapper<QcItemTemplate>(entity);
        lqw.orderByDesc("create_time");
        IPage<QcItemTemplate> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询检测模板-产品检测汇总
     *
     * @param entity 检测模板-产品检测汇总
     * @return 检测模板-产品检测汇总
     */
    @Override
    public List<QcItemTemplate> queryList(QcItemTemplate entity) {
        QueryWrapper<QcItemTemplate> lqw = new QueryWrapper<QcItemTemplate>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public QcItemTemplate getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增检测模板-产品检测汇总
     *
     * @param entity 检测模板-产品检测汇总
     * @return 结果
     */
    @Override
    public void insert(QcItemTemplate entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改检测模板-产品检测汇总
     *
     * @param entity 检测模板-产品检测汇总
     * @return 结果
     */
    @Override
    public boolean update(QcItemTemplate entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除检测模板-产品检测汇总
     *
     * @param recordIds 需要删除的检测模板-产品检测汇总主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除检测模板-产品检测汇总信息
     *
     * @param recordId 检测模板-产品检测汇总主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<QcItemTemplate> buildQueryWrapper(QcItemTemplate entity) {
        LambdaQueryWrapper<QcItemTemplate> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(entity.getItemCode()), QcItemTemplate::getItemCode, entity.getItemCode());
                    lqw.like(StringUtils.isNotBlank(entity.getItemName()), QcItemTemplate::getItemName, entity.getItemName());
                    lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), QcItemTemplate::getSpecification, entity.getSpecification());
                    lqw.eq(StringUtils.isNotBlank(entity.getUnitOfMeasure()), QcItemTemplate::getUnitOfMeasure, entity.getUnitOfMeasure());
                    lqw.eq(entity.getQuantityCheck() != null, QcItemTemplate::getQuantityCheck, entity.getQuantityCheck());
                    lqw.eq(entity.getQuantityUnqualified() != null, QcItemTemplate::getQuantityUnqualified, entity.getQuantityUnqualified());
                    lqw.eq(entity.getCrRate() != null, QcItemTemplate::getCrRate, entity.getCrRate());
                    lqw.eq(entity.getMajRate() != null, QcItemTemplate::getMajRate, entity.getMajRate());
                    lqw.eq(entity.getMinRate() != null, QcItemTemplate::getMinRate, entity.getMinRate());
                    lqw.eq(entity.getCountTemplate() != null, QcItemTemplate::getCountTemplate, entity.getCountTemplate());
                    lqw.eq(entity.getCountUnqualified() != null, QcItemTemplate::getCountUnqualified, entity.getCountUnqualified());
                    lqw.eq(StringUtils.isNotBlank(entity.getStatus()), QcItemTemplate::getStatus, entity.getStatus());
        return lqw;
    }
}
